package org.csu.mypetstore.api.controller.front;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.common.ResponseCode;
import org.csu.mypetstore.api.service.ChatService;
import org.csu.mypetstore.api.service.MessageService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.csu.mypetstore.api.vo.ChatUserVO;
import org.csu.mypetstore.api.vo.ContactDataVO;
import org.csu.mypetstore.api.vo.MessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api/chat/")
public class ChatController {
    @Autowired
    private ChatService chatService;
    @Autowired
    private MessageService messageService;

    //根据用户名的关键词找人
    @GetMapping("find_users")
    @ResponseBody
    public CommonResponse<List<ChatUserVO>> findUsers(@RequestParam("keyword") String keyword, HttpSession session){
        AccountVO loginAccount = (AccountVO) session.getAttribute("login_account");
        if(loginAccount == null)
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "请先登录！");
        return chatService.findUsers(keyword, loginAccount.getUsername());
    }

    //根据群的关键词找群(不包括用户已经进入的群)
    @GetMapping("find_groups")
    @ResponseBody
    public CommonResponse<List<ChatUserVO>> findGroups(@RequestParam("keyword") String keyword, HttpSession session){
        AccountVO loginAccount = (AccountVO) session.getAttribute("login_account");
        if(loginAccount == null)
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "请先登录！");
        return chatService.findGroups(keyword, loginAccount.getUsername());
    }

    //添加一位好友
    @PostMapping("add_friend")
    @ResponseBody
    public CommonResponse<ContactDataVO> addUser(@RequestParam("friendName") String friendName, HttpSession session){
        AccountVO loginAccount = (AccountVO) session.getAttribute("login_account");
        if(loginAccount == null)
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "请先登录！");
        return chatService.addUser(friendName, loginAccount.getUsername());
    }

    //进入一个群
    @PostMapping("enter_group")
    @ResponseBody
    public CommonResponse<ContactDataVO> enterGroup(@RequestParam("groupId") String groupId, HttpSession session){
        AccountVO loginAccount = (AccountVO) session.getAttribute("login_account");
        if(loginAccount == null)
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "请先登录！");
        return chatService.enterGroup(groupId, loginAccount.getUsername());
    }

    //创建一个群
    @PostMapping("create_group")
    @ResponseBody
    public CommonResponse<ContactDataVO> createGroup(@RequestParam("groupId") String groupId, @RequestParam("groupName") String groupName, HttpSession session){
        AccountVO loginAccount = (AccountVO) session.getAttribute("login_account");
        if(loginAccount == null)
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "请先登录！");
        return chatService.createGroup(groupId, groupName, loginAccount.getUsername());
    }

    //获取自己的好友和群列表
    @GetMapping("get_friends_and_groups")
    @ResponseBody
    public CommonResponse<List<ContactDataVO>> getFriendsAndGroups(HttpSession session){
        AccountVO loginAccount = (AccountVO) session.getAttribute("login_account");
        if(loginAccount == null)
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "请先登录！");
        return chatService.getFriendsAndGroups(loginAccount.getUsername());
    }

    //获取用户的历史信息：
    @GetMapping("historyMessages")
    @ResponseBody
    public CommonResponse<List<MessageVO>> getHistoryMessageById(HttpSession session){
        System.out.println("有用户请求历史消息");
        AccountVO accountVO = (AccountVO) session.getAttribute("login_account");
        if(accountVO == null)
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "请先登录！");
        return messageService.getHistoryMessage(accountVO.getUsername());
    }

    //用户添加的历史消息
    @PostMapping("add_history_message")
    @ResponseBody
    public CommonResponse<MessageVO> addHistoryMessage(
            @RequestParam("messageId") String messageId,
            @RequestParam("status")String status,
            @RequestParam("type")String type,
//            @RequestParam("sendTime") Date sendTime,
            @RequestParam("sendTime") String sendTimeString,
            @RequestParam("content") String content,
            @RequestParam("toContactId")String toContactId,
            @RequestParam("userId") String userId,
            @RequestParam("displayName") String displayName,
            @RequestParam("avatar") String avatar){
        ChatUserVO fromUser = new ChatUserVO(userId,displayName,avatar);
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sendTime = null;
        try {
            sendTime = sf.parse(sendTimeString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        MessageVO messageVO = new MessageVO(messageId,status,type,sendTime,content,toContactId,fromUser);
        return messageService.updateHistoryMessage(messageVO);
    }
}
